import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CMProcessesInfo {

    public static void main(String[] args) {
        // Set Cloudera Manager API endpoint and credentials
        String cmHost = "10.87.229.25";
        int cmPort = 7180;
        String apiVersion = "v33";
        String username = "admin";
        String password = "admin";

        // Set the host ID for which to retrieve process information
        String hostId = "738b5d2c-678a-49a5-82f2-b63c5034b289";
//        http://cdh001:7180/api/v33/hosts/738b5d2c-678a-49a5-82f2-b63c5034b289
        // Build API call URL to get host process information
        String apiCallUrl = null;
        try {
            apiCallUrl = new URIBuilder()
                    .setScheme("http")
                    .setHost(cmHost)
                    .setPort(cmPort)
//                    .setPath("/api/" + apiVersion + "/hosts/" + hostId + "/processes")
                    .setPath("/api/" + apiVersion + "/hosts/" + hostId)
                    .build()
                    .toString();
        } catch (Exception e) {
            System.out.println("Error building API URL: " + e.getMessage());
            return;
        }

        // Set up HTTP client with Cloudera Manager credentials
        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();

        // Execute HTTP GET request to get process information for host
        HttpGet httpGet = new HttpGet(apiCallUrl);
        httpGet.setHeader("Accept", "application/json");
        List<String> processNames = new ArrayList<String>();
        List<Integer> processIds = new ArrayList<Integer>();
        try {
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                String responseString = EntityUtils.toString(entity, "UTF-8");
                JSONObject jsonObject = new JSONObject(responseString);
                JSONArray processes = jsonObject.getJSONArray("process");
                for (int i = 0; i < processes.length(); i++) {
                    JSONObject process = processes.getJSONObject(i);
                    processNames.add(process.getString("name"));
                    processIds.add(process.getInt("processId"));
                }
            } else {
                System.out.println("Error retrieving process information. HTTP status code: "
                        + response.getStatusLine().getStatusCode());
                return;
            }
        } catch (Exception e) {
            System.out.println("Error retrieving process information: " + e.getMessage());
            return;
        }

        // Print the process names and IDs
        for (int i = 0; i < processNames.size(); i++) {
            System.out.println("Process Name: " + processNames.get(i) + ", Process ID: " + processIds.get(i));
        }
    }

}
